using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;




namespace device_query_tool
{
    public class Utils {
        public static string[] getBoardIP(string ipAddress, string subnetMask, ref int countIp)
        {
            // 统计子网掩码中 1 的数量
            string[] mask = subnetMask.Split('.');
            for (int i = 0; i < 3; i++)
            {
                mask[i] = Convert.ToString(Convert.ToInt32(mask[i]), 2);
            }

            int countOne = 0;
            foreach (char i in mask[0] + mask[1] + mask[2] + mask[3])
            {
                if (String.Equals(i, '1'))
                {
                    countOne++;
                }
            }

            // 将 ip 地址转化为二进制字符串形式
            string[] ipAddressBit = ipAddress.Split('.');

            for (int i = 0; i < 4; i++)
            {
                ipAddressBit[i] = Convert.ToString(Convert.ToInt32(ipAddressBit[i]), 2);
                // 填 0 占位操作
                if (ipAddressBit[i].Length < 8)
                {
                    var builder1 = new StringBuilder();
                    for (int j = 0; j < 8 - ipAddressBit[i].Length; j++)
                    {
                        builder1.Append("0");
                    }
                    ipAddressBit[i] = builder1 + ipAddressBit[i];
                }
            }

            // 得到的二进制 ip 地址
            string ipBit = ipAddressBit[0] + ipAddressBit[1] + ipAddressBit[2] + ipAddressBit[3];
            ipBit = ipBit.Substring(0, countOne);

            int countZero = 32 - countOne;
            countIp = Convert.ToInt32(Math.Pow(2, countZero));
            var builder = new StringBuilder();
            // 如果子网掩码对应的为 1，则对应位保持原样
            for (int i = 0; i < countOne; i++)
            {
                builder.Append(ipBit[i]);
            }

            string[] resultIPs = new string[countIp];
            // 遍历子网所有 ip 地址并添加进返回数组
            for (int i = 0; i < countIp; i++)
            {
                string temp = Convert.ToString(i, 2);
                // 添加前置 0 操作
                if (temp.Length <= countZero)
                {
                    var builder2 = new StringBuilder();
                    for (int j = 0; j < countZero - temp.Length; j++)
                    {
                        builder2.Append("0");
                    }
                    //得到 ip 地址的二进制表示
                    temp = ipBit + builder2 + temp;
                }

                // 转换为二进制地址形式
                resultIPs[i] =
                    Convert.ToInt32(temp.Substring(0, 8), 2).ToString() + "." +
                    Convert.ToInt32(temp.Substring(8, 8), 2).ToString() + "." +
                    Convert.ToInt32(temp.Substring(16, 8), 2).ToString() + "." +
                    Convert.ToInt32(temp.Substring(24, 8), 2).ToString();

            }


            return resultIPs;
        }


        // 远程子网广播地址计算
        public static string GetBroadcast(string ipAddress, string subnetMask)
        {

            byte[] ip = IPAddress.Parse(ipAddress).GetAddressBytes();
            byte[] sub = IPAddress.Parse(subnetMask).GetAddressBytes();

            // 广播地址=子网按位求反 再 或IP地址
            for (int i = 0; i < ip.Length; i++)
            {
                ip[i] = (byte)((~sub[i]) | ip[i]);
            }
            return new IPAddress(ip).ToString();
        }


        public static String byteToString(byte[] vs, int begin, int end)
        {
            String resultString = null;
            byte[] result = vs.Skip(begin).Take(end - begin).ToArray();
            resultString += Encoding.ASCII.GetString(result);
            return resultString;
        }

        public static string getHostNameByIP(String ip)
        {
            // 获取主机名
            IPHostEntry myscanhost = Dns.GetHostEntry(
            IPAddress.Parse(ip));
            string name = myscanhost.HostName.ToString();
            return name;
        }

    }
}